package xFGE.xDisplay.BitmapEx
{
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.getTimer;
	
	public class BitmapAnimate extends BitmapSprite
	{
		public static const ANIMATE_STATE_STOP:String = "stop";
		
		public static const ANIMATE_STATE_PLAY:String = "play";
		
		public static const ANIMATE_STATE_PAUSE:String = "pause";
		
		public static const ANIMATE_TYPE_FORWARD:String = "forward";	
		
		public static const ANIMATE_TYPE_BACKWARD:String = "backward";
		
		private var _fps:int;
		
		private var _frameAcount:int;
		
		private var _currentFrame:int;
		
		private var _state:String;
		
		private var _type:String;
		
		private var _frameWidth:int;
		
		private var _frameHeight:int;
		
		private var _lastFrameTime:int;
		
		private var _frameSpeed:int;
		
		
		
		
		
		/**
		 * 动画精灵类
		 * @param x
		 * @param y
		 * @param w
		 * @param h
		 * @param texture
		 * @param fps
		 * @param transparent
		 * @param color
		 * @param pixelSnapping
		 * @param smoothing
		 * 
		 */
		public function BitmapAnimate(x:int, y:int, w:int, h:int, texture:BitmapData,fps:int, 
																		transparent:Boolean=true, 
																		color:int=0x00000000, 
																		pixelSnapping:String="auto", 
																		smoothing:Boolean=false)
		{
			//TODO: implement function
			super(x, y, w, h, texture, transparent, color, pixelSnapping, smoothing);
			_fps = fps;
			_frameAcount = _texture.width / w;
			_state= BitmapAnimate.ANIMATE_STATE_STOP;
			_type = BitmapAnimate.ANIMATE_TYPE_FORWARD;
			_frameWidth = w;
			_frameHeight = h;
			_frameSpeed = 1000/fps;
			_lastFrameTime = -1;
		}
		
		/**
		 * 播放动画 
		 * 
		 */
		public function Play():void
		{
	
			_lastFrameTime = getTimer();
			_setFrame(0);
			addEventListener(Event.ENTER_FRAME,_onEnterFrame);
			return;	
		}
		
		/**
		 * 判断是否在播放 
		 * @return 
		 * 
		 */
		public function isPlaying():Boolean
		{
			if(_state == BitmapAnimate.ANIMATE_STATE_PLAY)return true;
			return false;
		}
		
		/**
		 * 停止播放动画 
		 * 
		 */
		public function Stop():void
		{
			removeEventListener(Event.ENTER_FRAME,_onEnterFrame);
			return;
		}
		
		/**
		 * 停留在某一帧 
		 * @param frame
		 * 
		 */
		public function gotoAndStop(frame:int):void
		{
			Stop();
			_setFrame(frame);
			addEventListener(Event.ENTER_FRAME,_onEnterFrame);
		}
		
		/**
		 * 从某一帧开始播放 
		 * @param frame
		 * 
		 */
		public function gotoAndPlay(frame:int):void
		{
			Stop();
			_setFrame(frame);
			addEventListener(Event.ENTER_FRAME,_onEnterFrame);
			
		}
		
		private function _onEnterFrame(event:Event):void
		{
			var delayTime:int = getTimer() - _lastFrameTime;
			
		 	if(delayTime >= _frameSpeed){
		 		_currentFrame++;
				_setFrame(_currentFrame);
				_lastFrameTime += delayTime;
			} 
			
		}
		
		/**
		 * 设置Frame 
		 * @param frame
		 * 
		 */
		private function _setFrame(frame:int):void
		{
			
			frame = frame % _frameAcount
			_textureRect.x = frame * _frameWidth;
			super.draw();
			
		}
	}
}